<?php
/**
 * 文章列表 
 * 
 + ----------------------------------------------------- 
 * Date 2013-3-8
 + -----------------------------------------------------
 */
class PhotoAction extends BaseAction {
	
	public function index(){
		//当前位置
		$this->assign('position',$this->_getPosition());
		//推荐3
		$this->assign('recom_hunsha',$this->getArticle(3,10,'',true));
		$this->assign('recom_hunli',$this->getArticle(3,11,'',true));
		//列表
		$model=M('Article'); 
		$mid=isset($_REQUEST['mid']) ? intval($_REQUEST['mid']) : 0;
		$tag=empty($_REQUEST['tag']) ? '' : urldecode($_REQUEST['tag']);
		$k=empty($_REQUEST['k']) ? '' : urldecode($_REQUEST['k']);
		$map=array(
    		'a.status'	=>	1,	
    	);
    	if($mid){
			$Menu=D('Menu');
    		$map['a.menu_id'] = array('in',$Menu->getSubmenuId($mid));
    	}
		if($tag){
			$art_ids=$this->getRecordIds($tag,'Article');
			if(!empty($art_ids)){
				$map['a.id']=array('in',$art_ids);
			}
		}
		if($k){
			$map['a.title']=array("like",'%'.$k.'%');
		}
		
		$this->_list($model,$map,6,'add_time',false);
		if($tag||$k){
			$title=empty($tag) ? '' : '关于'.$tag.'的文章 - ';
			$title .=empty($k) ? '' : '关于'.$k.'的文章 - ';
			$title .= C('SITE_TITLE');
			$this->assign('title',$title);
		}
		
		$model=M('Menu');
		$simple=$model->find($mid);
		$this->assign('simple',$simple);
		$this->display();
	}
	
	/**
	 * 文章详细 
	 */
    public function read(){
		$id=isset($_REQUEST['id']) ? intval($_REQUEST['id']) : '';
		//当前位置
		$this->assign('position',$this->_getPosition());
		//文章热门标签云
		//$this->assign('arts_tag',$this->_getTags(85));
		//友情链接
		//$this->assign('friend_link',$this->_getFriendLink(0,-1));
		if($id){
			$Article=M("Article");
			//更新次数
			$Article->where('id='.$id)->setInc('view_times');
			$art=$Article->table(C("DB_PREFIX").'article a')
    					->join('left join '.C("DB_PREFIX").'menu m on(a.menu_id=m.id)')
    					->field('a.*,m.menu_name')
    					->where('a.id='.$id)->find();
			//$art['tags']=empty($art['tags']) ? array() : @explode(' ',$art['tags']);
			$this->assign('data',$art);
			if($art['id']){
				$model=M('Menu');
				$current_menu=$model->find($art['menu_id']);
				$this->assign('simple',$current_menu);
				//下一条
				$this->assign('next',$this->getNext($art));
				//上一条
				$this->assign('prev',$this->getPrev($art));
				//相关文章
				//$this->assign('like_arts',$this->getArticle(6,$art['menu_id']));
				//推荐文章
				//$this->assign('recom_arts',$this->getArticle(6,$art['menu_id'],'',true));
				//热门新闻
				//$this->assign('hot_arts',$this->getArticle(10,$art['menu_id'],'view_times desc'));
				//图片新闻
				//$this->assign('pic_arts',$this->getArticle(4,$art['menu_id'],'view_times desc',false,false,true));
				//推荐学校
				//$this->assign('recom_school',$this->getArticle(8,5,'',true));
				//评论
				$Comment=M("Comment");
				$map['record_id']	=	$id;
				$map['model']		=	$this->getActionName();
				$count=$Comment->where($map)->count();
				if($count > 0){
					$comments=$Comment->where($map)->order('add_time asc')->select();
					$this->assign('comments',$comments);
				}
				$this->assign('count',$count);
				
				$title = $art['title'].'-'.$art['menu_name'];
				$title .= ' - '.C('SITE_TITLE');
				$this->assign('title',$title);
				$this->display();
			}else{
				$this->_404('您查找的文章不存在，正为你跳转到首页！','__APP__/',1);
			}
		}else{
			$this->error('操作错误，正为你跳转到首页！','__APP__/');
		}
    }
    
    /**
     * 获取上一篇
     * @param array $curr
     */
    public function getPrev($curr){
    	$model=M('Article');
    	if(is_array($curr)){
    		$map=array(
    			'menu_id'	=>	$curr['menu_id'],
    			'add_time' 	=>	array('gt',$curr['add_time'])
    		);
    		$prev=$model->where($map)->order('add_time asc')->find();
    		return $prev;
    	}else{
    		return array();
    	}
    }
    
    /**
     * 获取下一篇
     * @param array $curr
     */
    public function getNext($curr){
    	$model=M('Article');
    	if(is_array($curr)){
    		$map=array(
    				'menu_id'	=>	$curr['menu_id'],
    				'add_time' 	=>	array('lt',$curr['add_time'])
    		);
    		$next=$model->where($map)->order('add_time desc')->find();
    		return $next;
    	}else{
    		return array();
    	}
    }
	
	
	protected function _list($model = '', $map = '', $page_size=15, $sortBy = '', $asc = false) {
        $model = empty($model) ? M($this->getActionName()) : $model;
        if (isset($_REQUEST['_order'])) {
            $order = 'a.'.$_REQUEST['_order'];
        } else {
            $order = !empty($sortBy) ? 'a.'.$sortBy : 'a.'.$model->getPk();
        }
        if (isset($_REQUEST['_sort'])) {
            $sort = $_REQUEST['_sort'] ? 'asc' : 'desc';
        } else {
            $sort = $asc ? 'asc' : 'desc';
        }
        $map['a.status']	=	1;
        //记录数
        $count = $model->table(C("DB_PREFIX").'article a')
        			->join('left join '.C('DB_PREFIX').'menu m on(a.menu_id=m.id)')
					->join('left join '.C('DB_PREFIX').'area area on(a.area_id=area.id)')
			    	->field('a.id,m.menu_name')
			    	->where($map)
					->count('a.id');
        //echo $model->getLastSql();exit;
        if ($count > 0) {
            //分页显示
            import("ORG.Util.Page");
            $p = new Page($count, $page_size);
			//$p->rollPage=10;
            $page = $p->show();
			$list=$model->table(C("DB_PREFIX").'article a')
					->join('left join '.C('DB_PREFIX').'menu m on(a.menu_id=m.id)')
					->join('left join '.C('DB_PREFIX').'area area on(a.area_id=area.id)')
			    	->field('a.*,m.menu_name')
			    	->where($map)
			    	->order($order . ' ' . $sort)
			    	->limit($p->firstRow. ','.$p->listRows)
			    	->select();
			//echo $model->getLastSql();exit;
            $this->assign('page', $page);
			$new_list=array();
			foreach($list as $k=>$v){
				$new_list[$k]=$v;
				$tag=empty($_REQUEST['tag']) ? '' : urldecode($_REQUEST['tag']);
				$kw=empty($_REQUEST['k']) ? '' : urldecode($_REQUEST['k']);
				if($tag){
					$new_list[$k]['title']=preg_replace('/'.$tag.'/','<font color=red>'.$tag.'</font>',$v['title']);
				}elseif($kw){
					$new_list[$k]['title']=preg_replace('/'.$kw.'/','<font color=red>'.$kw.'</font>',$v['title']);
				}
				$new_list[$k]['tags']=empty($v['tags']) ? array() : @explode(' ',$v['tags']);
			}
			//dump($new_list);exit;
            $this->assign('list', $new_list);
        }
		return;
    }
}
?>
